using System;
using System.Data;
using System.Windows.Forms;
using System.IO;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the Countries table. It enables loading from a file,
    ///saving to a file and more
    /// </summary>
    public partial class CountriesEditor : UserControl
    {
        public DSIPAMS.DTCountriesDataTable countries;
        string[] countriesHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="CountriesEditor"/> class.
        /// </summary>
        public CountriesEditor()
        {
            InitializeComponent();
            InitializeComponentExtneded();
            InitializeTables();
        }

        /// <summary>
        /// Initializes the tables.
        /// </summary>
        private void InitializeTables()
        {
            dgvCountries.Columns[0].HeaderText = "Country Symbol";
            dgvCountries.Columns[1].HeaderText = "Country Name";
            dgvCountries.Columns[2].HeaderText = "Country Currency";


           
            foreach (DataGridViewColumn dc in dgvCountries.Columns)
            {
                dc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
        }


        /// <summary>
        /// Initializes the component extneded.
        /// </summary>
        private void InitializeComponentExtneded()
        {
           countries = new DSIPAMS.DTCountriesDataTable();
           dgvCountries.DataSource = countries;
        }

        /// <summary>
        /// Handles the DataSourceChanged event of the dgvCountries control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvCountries_DataSourceChanged(object sender, EventArgs e)
        {
            dgvCountries.Refresh();
        }

        /// <summary>
        /// Loads the data from a file .
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns>a DataTable with the file's data</returns>
        public DataTable LoadData(string dataFilePath)
        {

            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                countriesHeaders = sr.ReadLine().Split(',');
                ;
                while (true)
                {
                    string country = sr.ReadLine();
                    if (country == string.Empty || country == null) break;
                    string[] countryInfo = country.Split(',');
                    if (countryInfo.Length != 3) continue;
                    countries.AddDTCountriesRow(countryInfo[0], countryInfo[1], countryInfo[2]);
                }
                sr.Close();
                Text = dataFilePath;

                countries.AcceptChanges();
                dgvCountries.DataSource = countries;
                for (int index = 0; index < countriesHeaders.Length; index++)
                {
                    dgvCountries.Columns[index].HeaderText = countriesHeaders[index];
                }
                foreach (DataGridViewColumn column in dgvCountries.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }
         
           
            return countries;
        }

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = countriesHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTCurrenciesRow country in countries)
            {
                fileContent += string.Format("{0},{1},{2}" + Environment.NewLine, country[0], country[1], country[2]);
            }
            sw.Write(fileContent);
            sw.Close();

        }

    }
   
}
